import request from '@/utils/request'
const API_PREFIX = '/service-system/api/v1/dict'

import { Dict } from './types.ts'
import { Page, Option } from '../../types.ts'

/**
 * 分页获取字典列表
 * @param params
 * @returns
 */
export const getDictList = (typeCode: string) => {
    return request.get<Dict.Row[]>(`${API_PREFIX}/${typeCode}/list`)
}

/**
 * 获取字典表单详情
 * @param id
 * @returns
 */
export const getDictDetail = (id: number) => {
    return request.get<Dict.Form>(`${API_PREFIX}/${id}/form`)
}

/**
 * 保存字典
 * @param params
 * @returns
 */
export const saveDict = (params: Dict.Form) => {
    return request.post(`${API_PREFIX}`, params)
}

/**
 * 更新字典
 * @param params
 * @returns
 */
export const updateDict = (params: Dict.Form) => {
    return request.put(`${API_PREFIX}/${params.id}`, params)
}

/**
 * 删除字典
 * @param idStr 字典ID字符串，多个应为逗号‘，’分割
 * @returns
 */
export const deleteDict = (idStr: string) => {
    return request.delete(`${API_PREFIX}/${idStr}`)
}

/**
 * 获取字典下拉
 * @returns
 */
export const getDictOptions = () => {
    return request.get<Array<Option>>(`${API_PREFIX}/options`)
}

/**
 * 分页获取字典类型列表
 * @param params
 * @returns
 */
export const getDictTypeList = (params: Dict.Type.Query) => {
    return request.get<Page<Dict.Type.Row>>(`${API_PREFIX}/type/page`, {
        params,
    })
}

/**
 * 获取字典表单详情
 * @param id
 * @returns
 */
export const getDictTypeDetail = (id: number) => {
    return request.get<Dict.Type.Form>(`${API_PREFIX}/type/${id}/form`)
}

/**
 * 保存字典类型
 * @param params
 * @returns
 */
export const saveDictType = (params: Dict.Type.Form) => {
    return request.post(`${API_PREFIX}/type`, params)
}

/**
 * 更新字典
 * @param params
 * @returns
 */
export const updateDictType = (params: Dict.Type.Form) => {
    return request.put(`${API_PREFIX}/type/${params.id}`, params)
}

/**
 * 删除字典
 * @param idStr 字典ID字符串，多个应为逗号‘，’分割
 * @returns
 */
export const deleteDictType = (idStr: string) => {
    return request.delete(`${API_PREFIX}/type/${idStr}`)
}
